Skip to content

fix: no-op clients without api key#544

Merged
marandaneto merged 1 commit intoPostHog:mainfrom
marandaneto:fix/disabled-no-api-key
Apr 30, 2026
Merged

fix: no-op clients without api key#544
marandaneto merged 1 commit intoPostHog:mainfrom
marandaneto:fix/disabled-no-api-key

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented Apr 30, 2026

💡 Motivation and Context

Fixes #173.

When a direct client is created with a project API key that is missing or trims to an empty string, the SDK now logs the empty-key warning and marks that client as disabled so calls are no-ops. Feature flag loading and direct flag decision APIs also return early for disabled clients to avoid malformed requests with an empty token.

Module-level setup now preserves the client’s auto-disabled state after trimming a whitespace-only key.

💚 How did you test it?

  • uv run pytest posthog/test/test_client.py posthog/test/test_module.py -q
  • uv run ruff format --check posthog/__init__.py posthog/client.py posthog/test/test_client.py posthog/test/test_module.py
  • uv run ruff check posthog/__init__.py posthog/client.py posthog/test/test_client.py posthog/test/test_module.py

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file
  • Added the release label to the PR

@marandaneto marandaneto force-pushed the fix/disabled-no-api-key branch from f47fb24 to e955b1c Compare April 30, 2026 05:46
@marandaneto marandaneto changed the title fix: allow disabled clients without api key fix: no-op clients without api key Apr 30, 2026
@marandaneto marandaneto force-pushed the fix/disabled-no-api-key branch from e955b1c to aa8c91f Compare April 30, 2026 05:53
@marandaneto marandaneto marked this pull request as ready for review April 30, 2026 05:54
@marandaneto marandaneto requested a review from a team as a code owner April 30, 2026 05:54
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/test/test_client.py:77-86
**Non-parameterised test duplicates existing parameterised case**

The `""` input here exercises the same code path and assertions (`api_key == ""`, `disabled == True`, same error-log message) already covered by the `whitespace_only` row in `test_trims_api_key_whitespace`. Per the team's preference for parameterised tests and the OnceAndOnlyOnce rule, adding a third row `("empty_string", "", "", True, True)` would be cleaner. The `capture()` no-op assertion is the only genuinely new thing here, and that could live in a focused test (or the parameterised test itself could grow the assertion).

```python
("empty_string", "", "", True, True),
```

Reviews (1): Last reviewed commit: "fix: allow disabled clients without api ..." | Re-trigger Greptile

Comment thread posthog/test/test_client.py Outdated
Copy link
Copy Markdown

@ioannisj ioannisj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, left a minor comment on disabled checks.

Comment thread posthog/client.py
@marandaneto marandaneto force-pushed the fix/disabled-no-api-key branch 4 times, most recently from 989087d to e19f82d Compare April 30, 2026 08:33
@marandaneto marandaneto force-pushed the fix/disabled-no-api-key branch from e19f82d to 53cddbb Compare April 30, 2026 08:39
@marandaneto marandaneto merged commit 8bdd3fa into PostHog:main Apr 30, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

posthog.project_api_key should be able to be None if (posthog.disabled == True)

2 participants